import { defHttp } from '@/utils/http';

enum Api {
  getList = '/v1/system/role',
  save = '/v1/system/role',
  del = '/v1/system/role',
  upStatus = '/v1/system/role/state',
  getAll = '/v1/system/role/all',
  UpdateInfo = '/v1/system/role',
  getApi = '/v1/system/role/api',
  getRule = '/v1/system/role/rule',
  updateApi = '/v1/system/role/api',
  updateRule = '/v1/system/role/rule',
}

//获取数据
export function getList(params: object) {
  return defHttp.get(
    { url: Api.getList, params: params },
    { errorMessageMode: 'none' }
  );
}

//新增
export function add(params: object) {
  return defHttp.post(
    { url: Api.save, params: params },
    { errorMessageMode: 'message' }
  );
}

//修改
export function update(params: object) {
  return defHttp.put(
    { url: Api.UpdateInfo, params: params },
    { errorMessageMode: 'message' }
  );
}

//删除数据
export function del(params: object) {
  return defHttp.delete(
    { url: Api.del, params: params },
    { errorMessageMode: 'message' }
  );
}

//更新状态
export function upStatus(params: object) {
  return defHttp.put(
    { url: Api.upStatus, params: params },
    { errorMessageMode: 'message' }
  );
}

//获取全部数据
export function getAllRole(params: object) {
  return defHttp.get(
    { url: Api.getAll, params: params },
    { errorMessageMode: 'message' }
  );
}

//获取全部数据
export function getApi(params: object) {
  return defHttp.get(
    { url: Api.getApi, params: params },
    { errorMessageMode: 'message' }
  );
}

//获取全部数据
export function getRule(params: object) {
  return defHttp.get(
    { url: Api.getRule, params: params },
    { errorMessageMode: 'message' }
  );
}

//获取全部数据
export function updateApi(params: object) {
  return defHttp.put(
    { url: Api.updateApi, params: params },
    { errorMessageMode: 'message' }
  );
}

//获取全部数据
export function updateRule(params: object) {
  return defHttp.put(
    { url: Api.updateRule, params: params },
    { errorMessageMode: 'message' }
  );
}

/**数据类型 */
export interface DataItemRole {
  id: number;
  name: string;
}

// 提取所有节点 id 的递归函数
export function getAllRuleIds(list: any[]): number[] {
  const ids: number[] = [];

  function traverse(nodes: any[]) {
    for (const node of nodes) {
      ids.push(node.id);
      if (node.children && node.children.length > 0) {
        traverse(node.children);
      }
    }
  }

  traverse(list);
  return ids;
}
